import datetime


class RoomUser(object):
  """Association between room and user.
  
  Attributes:
    * room
    * user
    * from_date
    * to_date
  """
  
  def __init__(self, room=None, user=None, from_date=None, to_date=None):
    """This is intended to be used by the association proxy of sqlalchemy.
    Either the attribute user or room is already set, the other one is provided.
    """
    if not room is None:
      assert user is None
      self.room = room
    elif not user is None:
      assert room is None
      self.user = user
    self.from_date = from_date or datetime.datetime.now()
    self.to_date = to_date
    
  def stop(self, now=None):
    """The user is not living in the room from today.
    """
    assert self.to_date is None
    self.user.active = False
    now = now or datetime.datetime.now()
    self.to_date = now
    
  def __repr__(self):
    return "<Room-user association %s %s %s (from %s to %s)>" % \
          (self.id, self.room, self.user, self.from_date, self.to_date)
          
  